package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.UpDownShelvesEnum;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCarousel;
import com.member.shop.domain.SysBizJingang;
import com.member.shop.mapper.SysBizJingangMapper;
import com.member.shop.service.ISysBizJingangService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 金刚区Service业务层处理
 *
 * @author lixinchen
 * @date 2024-12-27
 */
@Service
public class SysBizJingangServiceImpl extends ServiceImpl<SysBizJingangMapper, SysBizJingang> implements ISysBizJingangService {
    @Resource
    private SysBizJingangMapper sysBizJingangMapper;

    /**
     * 查询金刚区
     *
     * @param jingangId 金刚区主键
     * @return 金刚区
     */
    @Override
    public SysBizJingang selectSysBizJingangByJingangId(Long jingangId) {
        return sysBizJingangMapper.selectById(jingangId);
    }

    /**
     * 查询金刚区列表
     *
     * @param queryWrapper 金刚区
     * @return 金刚区
     */
    @Override
    public List<SysBizJingang> selectSysBizJingangList(LambdaQueryWrapper<SysBizJingang> queryWrapper) {
        return sysBizJingangMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysBizJingang> selectSysBizJingangList(SysBizJingang sysBizJingang) {
        return sysBizJingangMapper.selectSysBizJingangList(sysBizJingang);
    }

    /**
     * 新增金刚区
     *
     * @param sysBizJingang 金刚区
     * @return 结果
     */
    @Override
    public int insertSysBizJingang(SysBizJingang sysBizJingang) {
        sysBizJingang.setCreateTime(DateUtils.getNowDate());
        return sysBizJingangMapper.insert(sysBizJingang);
    }

    /**
     * 修改金刚区
     *
     * @param sysBizJingang 金刚区
     * @return 结果
     */
    @Override
    public int updateSysBizJingang(SysBizJingang sysBizJingang) {
        sysBizJingang.setUpdateTime(DateUtils.getNowDate());
        return sysBizJingangMapper.updateById(sysBizJingang);
    }

    /**
     * 批量删除金刚区
     *
     * @param jingangIds 需要删除的金刚区主键
     * @return 结果
     */
    @Override
    public int deleteSysBizJingangByJingangIds(Long[] jingangIds) {
        return sysBizJingangMapper.deleteBatchIds(Arrays.asList(jingangIds));
    }

    /**
     * 删除金刚区信息
     *
     * @param jingangId 金刚区主键
     * @return 结果
     */
    @Override
    public int deleteSysBizJingangByJingangId(Long jingangId) {
        return sysBizJingangMapper.deleteById(jingangId);
    }

    /**
     * 查询金刚区的数量是否超过十个
     *
     * @return 结果
     */
    @Override
    public boolean selectGroundingNum(SysBizJingang sysBizJingang) {
        LambdaQueryWrapper<SysBizJingang> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizJingang::getDisplayed, UpDownShelvesEnum.GROUNDING.getKey());
        queryWrapper.eq(SysBizJingang::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        if (sysBizJingang.getTenantId() != null && sysBizJingang.getTenantId() > 0) {
            queryWrapper.eq(SysBizJingang::getTenantId, sysBizJingang.getTenantId());
        }
        //同一个区域不能超过20个
        queryWrapper.eq(SysBizJingang::getRegion, sysBizJingang.getRegion());
        queryWrapper.ne(sysBizJingang.getJingangId() != null, SysBizJingang::getJingangId, sysBizJingang.getJingangId());
        List<SysBizJingang> sysBizJingangs = sysBizJingangMapper.selectList(queryWrapper);
        return sysBizJingangs.size() < 20;
    }

    //判断该商品是否被金刚区
    @Override
    public boolean hasExist(Long id) {
        LambdaQueryWrapper<SysBizJingang> sysBizJingangLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizJingangLambdaQueryWrapper.eq(SysBizJingang::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizJingangLambdaQueryWrapper.eq(SysBizJingang::getJumpType, "GOODS_LIST");
        List<SysBizJingang> sysBizJingangs = sysBizJingangMapper.selectList(sysBizJingangLambdaQueryWrapper);
        for (SysBizJingang sysBizJingang : sysBizJingangs) {
            String jumpContent = sysBizJingang.getJumpContent();
            Pattern pattern = Pattern.compile("id=(\\d+)");
            Matcher matcher = pattern.matcher(jumpContent);
            // 输出匹配到的id值
            if (matcher.find()) {
                if (Integer.parseInt(matcher.group(1)) == id) {
                    return true;
                }
            }
        }


        return false;
    }
}
